﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

namespace Purdue.Extensions
{
    public static class CollectionViewExtensions
    {
        public static ICollectionView CreateView(this IEnumerable collectionSource, Predicate<object> filter)
        {
            var view = collectionSource.CreateView();
            view.Filter = filter;
            return view;
        }

        public static ICollectionView CreateView(this IEnumerable collectionSource)
        {
            var source = new CollectionViewSource()
            {
                Source = collectionSource
            };
            return source.View;
        }

        public static ICollectionView CreateViewFromEnum(Type enumType)
        {
            if (enumType == null)
                throw new ArgumentNullException("enumType");

            if (enumType.IsEnum)
            {
                var values = Enum.GetValues(enumType);
                var view = values.CreateView();
                view.MoveCurrentToFirst();
                return view;
            }
            else
            {
                Trace.Fail("Invalid state.");
                return new List<string>().CreateView();
            }
        }

        public static CollectionViewGroup GetGroupFromCurrentItem(this ICollectionView view, object currentItem)
        {
            if ((view != null) && (currentItem != null))
            {
                object groupName = view.GroupDescriptions[0].GroupNameFromItem(currentItem, 0, CultureInfo.CurrentCulture);
                return view.Groups.Cast<CollectionViewGroup>().FirstOrDefault(group => group.Name.Equals(groupName));
            }

            return null;
        }

        public static int GetGroupIndexFromItemIndex(this ICollectionView view, int itemIndex)
        {
            if (view != null)
            {
                int groupIndex = 0;

                foreach (CollectionViewGroup group in view.Groups)
                {
                    if (itemIndex < group.ItemCount)
                    {
                        return groupIndex;
                    }

                    groupIndex++;
                    itemIndex -= group.ItemCount;
                }
            }

            return -1;
        }

        public static void MoveCurrentToItemOrFirst(this ICollectionView view, object item)
        {
            if (view == null)
                throw new ArgumentNullException("view");

            if ((item != null) && view.Contains(item))
            {
                view.MoveCurrentTo(item);
            }
            else
            {
                view.MoveCurrentToFirst();
            }
        }

        /// <summary>
        /// Determines whether the specified collection view is null or empty.
        /// </summary>
        /// <param name="self">The collection view.</param>
        /// <returns>
        /// 	<c>true</c> if the specified collection view is null or empty; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsNullOrEmpty(this ICollectionView self)
        {
            return (self == null) || self.IsEmpty;
        }
    }
}
